'use strict';
(function() {
    var dom = '<div id="LIST" class="list-box-widget">\
                <div id="LISTx0" style="" class="left-arrow"><img src="http://127.0.0.1:8000/custom-templates/images/home/xiangzuo.png"></div>\
                   <div id="LISTx1" class="" style="">\
                       <div id="LISTx1x0y0">\
                           <ul style="" id="LISTx1x0y0ul">\
                           </ul>\
                       </div>\
                   </div>\
                   <div id="LISTx0" style="" class="left-arrow"><img src="http://127.0.0.1:8000/custom-templates/images/home/xiangyou.png"></div>\
                </div>'
    var home_page = {};
    /* 模版翻页处理 */
    var pagesInfo = {};
    //推荐位数据计数
    var dataNum = 0;
    home_page.init = function() {
        home_page.get_list_data(API_URL+'list/list1/p2/data.json');
    };
    
    //获取推荐为数据
    home_page.get_list_data = function(url) {

        getApi(url, function(data) {
            var list_data = home_page.analysis_list_data(data);
            home_page.create_list(list_data.list_data)

            if (pagesInfo.totalpage == 1) {
                $('.page-span').html('');
            } else {
                $('.page-span').html(pagesInfo.nowpage + ' / ' + pagesInfo.totalpage)

            }
        })
    };
    //解析推荐位数据
    home_page.analysis_list_data = function(data) {
        var list_data = {};
        list_data.list_data = data.data;
        
        if (data.modelInfo.page) {
            list_data.pages = data.modelInfo.page;
            pagesInfo = list_data.pages
        }
        return list_data;
    };
    //生成推荐位
    home_page.create_list = function(data) {
        // 填入的数据个数
        dataNum = 0;

        for (var nowId in data) {
            $.each(data, function(index, item) {
                console.log(item)
                if (item.type == 'ul') {
                    $.each(item.data, function(index1, item1) {
                        if (index1 == '0') {
                            $('#LISTx1' + nowId + 'ul').html('<li class="ver-box-style"><img src="' + item1.img + '"><p>' + item1.title + '</p></li>')

                        } else {
                            $('#LISTx1' + nowId + 'ul').append('<li class="ver-box-style"><img src="' + item1.img + '"><p>' + item1.title + '</p></li>')
                        }
                        $('#LISTx1' + nowId + 'ul li').eq(index1).attr({
                            'data-id': 'LISTul-' + item1.$rowid,
                            'data-routeid': item1.onclick.route.tplID,
                            'data-routetype': item1.onclick.route.tplType,
                            'data-href': item1.onclick.href,
                            'tabindex': '-1'
                        })

                        dataNum++;
                    })

                }



            })

        }

    };



    window.initList001 = function() {
        $("#custom-template-wrap").html(dom);
        home_page.init();
    }

    //选择数据后填充页面
    window.fillArea = function(area, data) {
        data = JSON.parse(data);
        console.log(data)
        for (var nowId in data) {
            $.each(data, function(index, item) {
                console.log(item)
                if (item.type == 'ul') {
                    $.each(item.data, function(index1, item1) {
                        if (index1 == '0') {
                            $('#LISTx1' + nowId + 'ul').html('<li class="ver-box-style"><img src="' + item1.img + '"><p>' + item1.title + '</p></li>')

                        } else {
                            $('#LISTx1' + nowId + 'ul').append('<li class="ver-box-style"><img src="' + item1.img + '"><p>' + item1.title + '</p></li>')
                        }
                        $('#LISTx1' + nowId + 'ul li').eq(index1).attr({
                            'data-id': 'LISTul-' + item1.$rowid,
                            'data-routeid': item1.onclick.route.tplID,
                            'data-routetype': item1.onclick.route.tplType,
                            'data-href': item1.onclick.href,
                            'tabindex': '-1'
                        })

                    })

                }



            })

        }
    };

})();
